<?php  
class ModuleReview extends Controller {
	function fetch() {
		$config   =& $this->locator->get('config');
		$currency	=& $this->locator->get('currency');
		$database =& $this->locator->get('database');
		$image    =& $this->locator->get('image');
		$language =& $this->locator->get('language');
		$request  =& $this->locator->get('request');
		$tax		=& $this->locator->get('tax');
		$url      =& $this->locator->get('url');
		$template =& $this->locator->get('template');
		
		if ($config->get('review_status')) {	
    		if (($request->gethtml('controller') == 'product') && ($request->gethtml('product_id'))) {
      			$review_info = $database->getRow("select r.review_id, r.rating, r.text, p.product_id, p.price, p.tax_class_id, pd.name, i.filename from review r left join product p on(r.product_id = p.product_id) left join product_description pd on(p.product_id = pd.product_id) left join image i on(p.image_id = i.image_id) where p.product_id = '" . (int)$request->gethtml('product_id') . "' and p.status = '1' and pd.language_id = '" . (int)$language->getId() . "' and p.date_available < now() and p.status = '1' and r.status = '1' order by rand() limit 1");
    		} else {
     			$review_info = $database->getRow("select r.review_id, r.rating, r.text, p.product_id, p.price, p.tax_class_id, pd.name, i.filename from review r left join product p on(r.product_id = p.product_id) left join product_description pd on(p.product_id = pd.product_id) left join image i on(p.image_id = i.image_id) where p.status = '1' and pd.language_id = '" . (int)$language->getId() . "' and p.date_available < now() and p.status = '1' and r.status = '1' order by rand() limit 1");
    		}	
		
			$language->load('extension/module/review.php');
    		 
			$view = $this->locator->create('template');

			$view->set('tpl', $template);
		
			if ($review_info) {
      			$view->set('heading_title', $language->get('heading_title'));
	
     			$view->set('text_rating', $language->get('text_rating', $review_info['rating']));
	
     			$view->set('name', $review_info['name']);
     			
     			$view->set('price', $currency->format($tax->calculate($review_info['price'], $review_info['tax_class_id'], $config->get('config_tax'))));

      			$view->set('rating', $review_info['rating']);

      			$view->set('description', trim(substr(strip_tags($review_info['text']), 0, 80)) . '...');
		
     			$view->set('image', $image->resize($review_info['filename'], $config->get('config_image_width'), $config->get('config_image_height')));

      			$view->set('review', $url->href('review_info', false, array('review_id' => $review_info['review_id'])));

   	   			$view->set('reviews', $url->href('review'));
	  			
				return $view->fetch('module/review.tpl');
    		} elseif ($request->gethtml('product_id')) {
      			$view->set('heading_title', $language->get('heading_title'));
	  
				$view->set('text_write', $language->get('text_write'));
 
      			$view->set('write', $url->href('review_write', false, array('product_id' => $request->gethtml('product_id'))));
				
				return $view->fetch('module/review.tpl');
    		}
		}
	}
}
?>
